<?php


require_once("classConnection.php");
require_once("ParamInOut.php");

class Model
{
	public function GetMenuList($IdCatalogue)
	{
		if($IdCatalogue == -1)
		{
			$strSql = "select iddanhmuc,Tendanhmuc from danhmuc Where Xoa=0";
		}
		else
		{
			$strSql = "select iddanhmuc,Tendanhmuc from danhmuc Where iddanhmuc = ".$IdCatalogue." and Xoa=0";
		}
		$db = new db();
		$data = $db->Execute($strSql);
		
		$menuArr = Array();
		while($row_menu = mysql_fetch_assoc($data))
		{
			$objMenu = new GetMenuListResponse();
			$objMenu->IdCatalogue = $row_menu['iddanhmuc'];
			$objMenu->MenuName = $row_menu['Tendanhmuc'];
			//$objMenu->NumOrder = $row_menu['ThuTu'];
			
			$menuArr[] = $objMenu;
		}
		return $menuArr;
	}
	
	
	public function CountProduct($idCatalogue,$new)
	{
		if($idCatalogue == -1)
		{
			$strSql = "select count(idsanpham)  from sanpham where new = ".$new." and Xoa = 0";
		}
		else
		{
			$strSql = "select count(idsanpham)  from sanpham where MaDM = ".$idCatalogue." and Xoa = 0";
		}	
		$db = new db();
		$data = $db->Execute($strSql);
		$count =	mysql_fetch_array($data);
		return $count[0]; 
	}
	
	public function GetProduct($idCatalogue,$pageSize,$pageStart,$new)
	{
		if($idCatalogue == -1)
		{
			$strSql = "select idSanPham,MaSanPham,TenSP,Gia,urlHinh,Hot,New from sanpham where new = ".$new." and Xoa = 0 ORDER BY NgayCapNhat DESC Limit ".$pageStart.",".$pageSize;
			//echo $strSql;
		}
		else
		{
			$strSql = "select * from sanpham where MaDM = ".$idCatalogue." and Xoa = 0 ORDER BY NgayCapNhat DESC Limit ".$pageStart.",".$pageSize;
			//echo $strSql;
		}
		
		$db = new db();
		$data = $db->Execute($strSql);
		
		$ProductArr = Array();
		while($row = mysql_fetch_assoc($data))
		{
			$obj = new GetProductResponse();
			$obj->IdProduct = $row['idSanPham'];
			$obj->ProductName = $row['TenSP'];
			$obj->ProductNo = $row['MaSanPham'];
			$obj->Rice = number_format($row['Gia'],'0','.','.');
			//$obj->DateModify = $row['NgayCapNhat'];
			$obj->UrlIamge = $row["urlHinh"];
			$obj->Hot = $row["Hot"];
			$obj->New = $row["New"];
			//$obj->Material = $row["ChatLieu"];
				
			$ProductArr[] = $obj;
		}
		return $ProductArr;
	}
	
	
	public function CountHotProduct()
	{
		
		$strSql = "select count(idSanPham)  from sanpham where Hot = 1 and Xoa = 0";
		//echo $strSql;
		$db = new db();
		$data = $db->Execute($strSql);
		$count =	mysql_fetch_array($data);
		return $count[0]; 
	}
	public function GetHotProduct($pageStart,$pageSize)
	{

		$strSql = "select idSanPham,MaSanPham,TenSP,Gia,urlHinh,Hot from sanpham where Hot = 1 and Xoa = 0 ORDER BY NgayCapNhat DESC Limit ".$pageStart.",".$pageSize;
		//echo $strSql;
		$db = new db();
		$data = $db->Execute($strSql);
		
		$ProductArr = Array();
		while($row = mysql_fetch_assoc($data))
		{
			$obj = new GetProductResponse();
			$obj->IdProduct = $row['idSanPham'];
			$obj->ProductName = $row['TenSP'];
			$obj->ProductNo = $row['MaSanPham'];
			$obj->Rice = number_format($row['Gia'],'0','.','.');
			//$obj->DateModify = $row['NgayCapNhat'];
			$obj->UrlIamge = $row["urlHinh"];
			$obj->Hot = $row["Hot"];
			//$obj->Material = $row["ChatLieu"];
				
			$ProductArr[] = $obj;
		}
		return $ProductArr;
	}
	
	public function CountSaleOffProduct()
	{
		
		$strSql = "select count(idSanPham)  from sanpham where KhuyenMai = 1 and Xoa = 0";
		//echo $strSql;
		$db = new db();
		$data = $db->Execute($strSql);
		$count =	mysql_fetch_array($data);
		return $count[0]; 
	}
	public function GetSaleOffProduct($pageStart,$pageSize)
	{

		$strSql = "select idSanPham,MaSanPham,TenSP,Gia,urlHinh,KhuyenMai from sanpham where KhuyenMai = 1 and Xoa = 0 ORDER BY NgayCapNhat DESC Limit ".$pageStart.",".$pageSize;
		//echo $strSql;
		$db = new db();
		$data = $db->Execute($strSql);
		
		$ProductArr = Array();
		while($row = mysql_fetch_assoc($data))
		{
			$obj = new GetProductResponse();
			$obj->IdProduct = $row['idSanPham'];
			$obj->ProductName = $row['TenSP'];
			$obj->ProductNo = $row['MaSanPham'];
			$obj->Rice = number_format($row['Gia'],'0','.','.');
			//$obj->DateModify = $row['NgayCapNhat'];
			$obj->UrlIamge = $row["urlHinh"];
			$obj->SaleOff = $row["KhuyenMai"];
			//$obj->Material = $row["ChatLieu"];
				
			$ProductArr[] = $obj;
		}
		return $ProductArr;
	}
	
	
	
	
	public function GetDetailProduct($idProduct)
	{

		$strSql = "select * from sanpham where idsanpham=".$idProduct." and Xoa=0 ORDER BY NgayCapNhat DESC" ;
		
		//echo $strSql;
		$db = new db();
		$data = $db->Execute($strSql);
		
		$ProductArr = Array();
		while($row = mysql_fetch_assoc($data))
		{
			$obj = new GetProductResponse();
			$obj->IdProduct = $row['idSanPham'];
			$obj->ProductNo = $row['MaSanPham'];
			$obj->ProductName = $row['TenSP'];
			$obj->Desciption = $row['MoTa'];
			$obj->Rice = number_format($row['Gia'],'0','.','.');
			$obj->DateModify = $row['NgayCapNhat'];
			$obj->UrlIamge = $row["urlHinh"];
			$obj->Hot = $row["Hot"];
			$obj->New = $row["New"];
			$obj->Material = $row["ChatLieu"];
				
			$ProductArr[] = $obj;
		}
		return $ProductArr;
	}
	
}


?>